//
//  AppDelegate.m
//  RocketProject
//
//  Created by chen jiedan on 13-5-7.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "AppDelegate.h"
#import "GloableEngineService.h"
#import "TravelException.h"
#import "RCSearchMoreTagsVC.h"
#import "RCDiscountMainVC.h"
#import "BaiduMobStat.h"



@implementation AppDelegate

@synthesize tabBarController = _tabBarController;
@synthesize loginVC          = _loginVC;
@synthesize alertViewManager = _alertViewManager;
@synthesize tsAlertOverlayWindow = _tsAlertOverlayWindow;
@synthesize tsAlertViewController = _tsAlertViewController;

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [_tabBarController release];
    [_loginVC  release];
    [_tsAlertViewController release];
    [_tsAlertOverlayWindow release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    [[UIApplication sharedApplication]setStatusBarHidden:NO];
    [[GloableEngineService sharedInstance] initWithDefaultLocController];
    [[GloableEngineService sharedInstance] updataUserLocation];
    [[GloableEngineService sharedInstance] setUpLoginInfo];
    
    [self IOSFiveDefaultBackground];
    
    if (!_alertViewManager) {
        _alertViewManager = [[TSAlertViewManager alloc]init];
    }
    if (!_tsAlertViewController) {
        _tsAlertViewController = [[TSAlertViewController alloc] init];
        _tsAlertViewController.view.backgroundColor = [UIColor clearColor];
    }
    
    if (!_tsAlertOverlayWindow) {
        _tsAlertOverlayWindow = [[TSAlertOverlayWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
        _tsAlertOverlayWindow.alpha = 0.0;
        _tsAlertOverlayWindow.backgroundColor = [UIColor clearColor];
        _tsAlertOverlayWindow.rootViewController = _tsAlertViewController;
    }
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    _tabBarController = [[RocketTabBar alloc] init];
    [_tabBarController initRocketTableView];
    
    //百度移动统计
    BaiduMobStat *mobStat = [BaiduMobStat defaultStat];
    mobStat.enableExceptionLog = YES;
    mobStat.logStrategy = BaiduMobStatLogStrategyAppLaunch;

#ifdef ServerVersion
    [mobStat startWithAppId:@"647cb24638"];
    _loginVC   = [[RCServerLoginVC alloc] init];
    if ([[GloableEngineService sharedInstance] hasLogin]) {
        self.window.rootViewController = _tabBarController;
    }else{
        self.window.rootViewController = _loginVC;
    }
#else
    [mobStat startWithAppId:@"21ec9293dc"];
    self.window.rootViewController = _tabBarController;
    
#endif
    [self.window makeKeyAndVisible];
    return YES;
}


- (void) serverLoginSuccess
{
    self.window.rootViewController = _tabBarController;


}

- (void) serverLogoutSuccess
{
    self.window.rootViewController = _loginVC;
    [[GloableEngineService sharedInstance] cleanServerLoginInfo];
}



- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma 
#pragma mark navigation bar custom
#define kDefaultBackgroundImageFile    @"image_titlebar_bg.png"
- (void) IOSFiveDefaultBackground
{
    BOOL isIOS5 = [[[UIDevice currentDevice]systemVersion] intValue] >=5 ? YES:NO;
    
    if(isIOS5)
    {
        UIImage *theBackgroundImage = [UIImage imageNamed:kDefaultBackgroundImageFile];
//        theBackgroundImage = [theBackgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(20, 3, 20, 3)];
        [[UINavigationBar appearance] setBackgroundImage:theBackgroundImage forBarMetrics:UIBarMetricsDefault];
//        [self customizeAppearance];
        
        }
}


- (void) customizeAppearance {
    
//    UIImage *i1 = [[UIImage imageNamed:@"lv_album_photo_return"]
//                   resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];
    UIImage *i1 = [UIImage imageNamed:@"lv_album_photo_return"];
    UIImage *i2 = [[UIImage imageNamed:@"lv_album_photo_return"]
                   resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];
    
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1
                                                      forState:UIControlStateNormal
                                                    barMetrics:UIBarMetricsDefault];
    
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2
                                                      forState:UIControlStateNormal
                                                    barMetrics:UIBarMetricsLandscapePhone];
    
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1
                                                      forState:UIControlStateHighlighted
                                                    barMetrics:UIBarMetricsDefault];
    
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2
                                                      forState:UIControlStateHighlighted
                                                    barMetrics:UIBarMetricsLandscapePhone];
    
    [[UIBarButtonItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
      UITextAttributeTextColor,
      [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],
      UITextAttributeTextShadowColor,
      [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
      UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"AmericanTypewriter" size:0.0],
      UITextAttributeFont,
      nil]
                                                forState:UIControlStateNormal];
}

- (void)alertWithNetworkError
{
    TSAlert *alertsStruct = [[[TSAlert alloc] init] autorelease];
    alertsStruct.isDismissByClick = YES;
    alertsStruct.alertMessage = @"您的网络不通，请稍后重试！";
    alertsStruct.alertStyleCode = TSAlertViewStyleNetworkNotReachCommonErrCode;
    
    [_alertViewManager addAlertToArray:alertsStruct];
}

- (void)alertWithNetworkTimeoutError
{
    TSAlert *alertsStruct = [[[TSAlert alloc] init] autorelease];
    alertsStruct.isDismissByClick = YES;
    alertsStruct.alertMessage = @"加载超时，请稍后重试！";
    alertsStruct.alertStyleCode = TSAlertViewStyleNetworkNotReachCommonErrCode;
    
    [_alertViewManager addAlertToArray:alertsStruct];
}

- (void) alertWithCustomizeString:(NSString *)alertString{
    TSAlert *alertsStruct = [[[TSAlert alloc] init] autorelease];
    alertsStruct.isDismissByClick = YES;
    alertsStruct.alertMessage = alertString;
    alertsStruct.alertStyleCode = TSAlertViewStyleNetworkNotReachCommonErrCode;
    [_alertViewManager addAlertToArray:alertsStruct];
}

- (void)updateTheLocationInfo
{
    UINavigationController *naviController = [_tabBarController.viewControllers objectAtIndex:0 withDefaultValue:nil];
    for (UIViewController *vc in naviController.viewControllers) {
        if ([vc isKindOfClass:[RCDiscountMainVC class]]) {
            [((RCDiscountMainVC *)vc) updateLocationInfo:[GloableEngineService sharedInstance].locationInfo];
        }
    }
    
    

}

@end
